//
//  RegisterViewController.m
//  NetWorkSave
//
//  Created by 苗旭萌 on 15/11/17.
//  Copyright © 2015年 苗旭萌. All rights reserved.
//

#import "RegisterViewController.h"
#import <Parse/Parse.h>
#import "AppDelegate.h"
#import "ViewController.h"

@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passWordTextField;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
- (IBAction)LoginButtonClicked:(id)sender {
    
    // parse里 跟登录注册有关的类
    PFUser *user = [[PFUser alloc] init];
    // 在user里面保存用户名和密码
    user.username = self.nameTextField.text;
    user.password = self.passWordTextField.text;
    
    // 注册用户名的方法
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
        if (succeeded) {
            [PFUser logInWithUsernameInBackground:self.nameTextField.text password:self.passWordTextField.text block:^(PFUser * _Nullable user, NSError * _Nullable error) {
                if (user) {
                    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
                    
                    ViewController *viewVC = [[ViewController alloc] init];
                    
                    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:viewVC];
                    
                    app.window.rootViewController = navi;
                    
                    
                    NSUserDefaults *userSince = [NSUserDefaults standardUserDefaults];
                    
                    [userSince setObject:self.nameTextField.text forKey:@"userName"];
                    [userSince setObject:self.passWordTextField.text forKey:@"passWord"];
                    
                    [userSince setBool:YES forKey:@"isLogin"];
                    [userSince setObject:@"userID" forKey:user.objectId];
                }
            }];
        }
    
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
